<?php


namespace model;

/**
 * Description of TblPerson
 *
 * @author Damian
 */
class TblPerson extends Model{
    /**
     * @column
     * @type varchar
     * @length 30
     * @readwrite
     * @validate required, max(30), email
     * @label E-mail
     */
    public $Email;
    const EMAIL="Email";
    /**
     * @column
     * @type int
     * @length 11
     * @primary
     * @readwrite
     */
    public $PersonID;
    const ID="PersonID";
    /**
     * @column
     * @type varchar
     * @length 30
     * @readwrite
     * @validate required, min(3), max(30), alpha
     * @label Imię
     */
    public $Name;
    const NAME="Name";

    /**
     * @readwrite
     * @type varchar
     * @length 30
     * @column
     * @validate required, min(3), max(30), alpha
     * @label Nazwisko
     */
    public $Surname;
    const SURNAME="Surname";
    /**
     * @readwrite
     * @type varchar
     * @length 30
     * @column
     * @validate required, min(6)
     * @label Hasło
     */
    public $Password;
    const PASSWORD="Password";
    /**
     *@readwrite
     * @var string
     */
    public $PasswordRepeat;  
    /**
     *
     * @readwrite
     */
    public $PhoneNumber;
    const PHONE_NUMBER="PhoneNumber";
    /**
     * @readwrite
     */
    public $Notes;
    const NOTES="Notes";
    /**
     * @readwrite
     */
    public $RegisterDate;
    const REGISTER_DATE="RegisterDate";
    /**
     * @readwrite
     * @type boolean
     * @length 1
     * @column
     * @var bool <p>opisuje czy ma uprawnienia administacyjne</p>
     * @label Numer Telefonu
     */
    public $IsAdmin;
    const IS_ADMIN="IsAdmin";
    public function validate() {
        if(parent::validate()){
            $error = false;
            if ($this->Password==$this->PasswordRepeat){
                $this->Password= password_hash($this->Password, PASSWORD_DEFAULT);
                $error = false;
            }
            else{
                $this->errors["Password"]="Podane hasła różnią się";
                $error = true;
            }            
            return !$error;
        }
        return false;
    }
    public function generatePassword(){
        $result="";
        $letters=array();
        for ($x="A";$x<"Z";$x++){
            $letters[]=$x;
        }
        $letters[]="Z";
        for ($x="a";$x<"z";$x++){
            $letters[]=$x;
        }
        $letters[]="z";
        for ($x=0;$x<10;$x++){
            $letters[]="".$x;
        }
        $letters+=[62=>"#","@","_","-"];
        $used=array();
        for ($i=0;$i<10;$i++){
            $number = rand(0, 65);
            if (!in_array($number, $used)){
                $used[]=$number;
            }
            else {
                $number = rand(0, 65);
                $used[]=$number;
            }         
            $result.=$letters[$number];
        }
        return $result;
    }
}
